package com.lebang.medicalInquiry.controller;


import com.lebang.medicalInquiry.constant.ConsultationScheduleEnum;
import com.lebang.medicalInquiry.constant.ResponseEnum;
import com.lebang.medicalInquiry.constant.annotation.RateLimit;
import com.lebang.medicalInquiry.constant.annotation.Require;
import com.lebang.medicalInquiry.model.po.ConsultationSheet;
import com.lebang.medicalInquiry.model.po.ConsultationSheetSchedule;
import com.lebang.medicalInquiry.model.po.UserPatient;
import com.lebang.medicalInquiry.model.po.UserPatientCaseFile;
import com.lebang.medicalInquiry.model.vo.ConsultationSheetInfoVo;
import com.lebang.medicalInquiry.model.vo.ConsultationSheetVo;
import com.lebang.medicalInquiry.model.vo.UserTokenVo;
import com.lebang.medicalInquiry.response.Callback;
import com.lebang.medicalInquiry.response.JsonData;
import com.lebang.medicalInquiry.service.OrganizationService;
import com.lebang.medicalInquiry.utils.TextUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;

@Api(tags = "机构相关")
@RequestMapping("/api/organization")
@RestController
@Validated
public class OrganizationApi {


    @Autowired
    private OrganizationService organizationService;


    @Require
    @ApiOperation("查询机构运营人员的医生")
    @GetMapping(value = "/physician")
    public JsonData getPhysicianList(HttpServletRequest request, HttpServletResponse response
    ) {


        String uuid = request.getAttribute("uuid").toString();

        return Callback.success(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMemo(), organizationService.getPhysicianList(uuid));

    }


    @Require
    @ApiOperation("查询机构问诊单状态对应的数量")
    @GetMapping(value = "/consultation/schedule/number")
    public JsonData countConsultationSheetNumber(HttpServletRequest request, HttpServletResponse response,
                                                 @ApiParam(name = "status", value = "多个类型值", example = "wait_prepay,settle_bill")
                                                 @NotBlank(message = "status 不允许为空") @RequestParam(name = "status", required = false) String status
    ) {


        String uuid = request.getAttribute("uuid").toString();

        return Callback.success(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMemo(), organizationService.countConsultationSheetNumber(uuid, status));

    }

    @Require
    @GetMapping("/patient")
    @ApiOperation("查询用户的就诊人")
    public JsonData<List<UserPatient>> getUserPatientList(HttpServletRequest request, HttpServletResponse response,
                                                          @ApiParam(name = "ids", value = "多个就诊人id") @NotBlank(message = "ids 不能为空")
                                                          @RequestParam(value = "ids", required = false, defaultValue = "1") String ids,
                                                          @ApiParam(name = "uuid", value = "uuid") @NotBlank(message = "uuid 不能为空")
                                                          @RequestParam(value = "uuid", required = false, defaultValue = "1") String uuid,
                                                          @ApiParam(name = "page", value = "页码")
                                                          @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                                                          @ApiParam(name = "limit", value = "条数")
                                                          @RequestParam(value = "limit", required = false, defaultValue = "1") Integer limit) {


        return Callback.success(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMemo(), organizationService.getUserPatientList(uuid, ids, page, limit));

    }


    @Require
    @GetMapping("/patient/case")
    @ApiOperation("查询就诊人病例文件")
    public JsonData<List<UserPatientCaseFile>> getUserPatientCaseFileList(HttpServletRequest request, HttpServletResponse response,
                                                                          @ApiParam(name = "patientId", value = "就诊人员id") @NotNull(message = "就诊人员id 不能为空")
                                                                          @RequestParam(value = "patientId", required = false) Integer patientId,
                                                                          @ApiParam(name = "uuid", value = "uuid") @NotBlank(message = "uuid 不能为空")
                                                                          @RequestParam(value = "uuid", required = false, defaultValue = "1") String uuid,
                                                                          @ApiParam(name = "page", value = "页码")
                                                                          @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                                                                          @ApiParam(name = "limit", value = "条数")
                                                                          @RequestParam(value = "limit", required = false, defaultValue = "1") Integer limit) {


        return Callback.success(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMemo(), organizationService.getUserPatientCaseFileList(uuid, patientId, page, limit));

    }


    @Require
    @ApiOperation("问诊咨询记录")
    @GetMapping(value = "/consultation/sheet")
    public JsonData<List<ConsultationSheetInfoVo>> getConsultationSheet(HttpServletRequest request, HttpServletResponse response,
                                                                        @ApiParam(name = "status", value = "进度状态( 待预付:wait_prepay,已预付:have_prepay,待接诊:wait_on_call,\" +\n" +
                                                                                "            \"不接诊:not_on_call,已预约:have_already_reserved,已接诊:have_on_call,账单核算:settle_bill,结束 end)", example = "wait_prepay")
                                                                        @RequestParam(name = "status", required = false) String status,
                                                                        @ApiParam(name = "page", value = "页码")
                                                                        @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                                                                        @ApiParam(name = "limit", value = "条数")
                                                                        @RequestParam(value = "limit", required = false, defaultValue = "1") Integer limit
    ) {


        String uuid = request.getAttribute("uuid").toString();


        return Callback.success(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMemo(), organizationService.getConsultationSheet(uuid, status, page, limit));

    }


    @Require
    @PostMapping("/consultation/schedule")
    @ApiOperation("添加问诊单记录")
    public JsonData addConsultationScheduleList(HttpServletRequest request, HttpServletResponse response, @RequestBody ConsultationSheet consultationSheet) {

        String uuid = request.getAttribute("uuid").toString();
        consultationSheet.setOrganizationOperationUuid(uuid);
        if (TextUtil.isEmpty(consultationSheet.getUserPaitentId(), consultationSheet.getUuid())) {
            return Callback.fail(ResponseEnum.FAIL.getCode(), "问诊单uuid和就诊人id不予讯为空");
        }
        return Callback.success(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMemo(), organizationService.addConsultationScheduleList(consultationSheet));

    }


    @Require
    @GetMapping("/consultation/schedule/info/{sheetId}")
    @ApiOperation("查询问诊单的进度详情")
    public JsonData<List<ConsultationSheetSchedule>> getUserConsultationScheduleList(HttpServletRequest request, HttpServletResponse response,
                                                                                     @ApiParam(name = "sheetId", value = "问诊单的id") @NotNull(message = "sheetId 问诊单id不允许为空")
                                                                                     @PathVariable(value = "sheetId") Integer sheetId) {


        return Callback.success(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMemo(), organizationService.getConsultationScheduleList(sheetId));

    }

    @Require
    @GetMapping("/consultation/info/{sheetId}")
    @ApiOperation("查询问诊单的详情")
    public JsonData<ConsultationSheetInfoVo> getUserConsultationList(HttpServletRequest request, HttpServletResponse response,
                                                                     @ApiParam(name = "sheetId", value = "问诊单的id") @NotNull(message = "sheetId 问诊单id不允许为空")
                                                                     @PathVariable(value = "sheetId") Integer sheetId) {
        String uuid = request.getAttribute("uuid").toString();

        return Callback.success(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMemo(), organizationService.getConsultationSheetById(sheetId));

    }

    @RateLimit(key = "update_consultation_sheet:", capacity = 2, requestedTokens = 2)
    @Require
    @ApiOperation("添加对应的问诊单进度")
    @PutMapping(value = "/consultation/schedule")
    public JsonData modifyConsultationSheet(HttpServletRequest request, HttpServletResponse response,
                                            @RequestBody ConsultationSheetVo consultationSheetSchedule
    ) {


        String operatorUuid = request.getAttribute("uuid").toString();
        Integer consultationSheetId = consultationSheetSchedule.getConsultationSheetId();
        ConsultationSheet consultationSheet = organizationService.getById(consultationSheetId);
        if (consultationSheetSchedule.getConsultationSheetId() == null) {
            return Callback.fail(ResponseEnum.FAIL.getCode(), ResponseEnum.FAIL.getMemo(), "问诊单id为空");
        }

        if (!consultationSheet.getOrganizationOperationUuid().equals(operatorUuid)) {
            return Callback.fail(ResponseEnum.FAIL.getCode(), ResponseEnum.FAIL.getMemo(), "当前问诊单非当前机构人员所属");
        }

        // 获取当前问诊的进度
        ConsultationScheduleEnum nowStep = ConsultationScheduleEnum.valueOf(consultationSheet.getScheduleState());

        if (ConsultationScheduleEnum.wait_prepay.getCode().equals(nowStep.getCode())) {
            return Callback.fail(ResponseEnum.FAIL.getCode(), ResponseEnum.FAIL.getMemo(), "问诊单待支付,无法操作");
        }
        if (ConsultationScheduleEnum.settle_bill.getCode().equals(nowStep.getCode())) {
            return Callback.fail(ResponseEnum.FAIL.getCode(), ResponseEnum.FAIL.getMemo(), "问诊单等待用户结算,无法操作");
        }
        if (ConsultationScheduleEnum.end.getCode().equals(nowStep.getCode())) {
            return Callback.fail(ResponseEnum.FAIL.getCode(), ResponseEnum.FAIL.getMemo(), "问诊单流程已结束,请勿重复操作");
        }
        // 获取要修改的问诊进度
        ConsultationScheduleEnum nextStep = ConsultationScheduleEnum.valueOf(consultationSheetSchedule.getScheduleState());
        if (nowStep.getCode().equals(nextStep.getCode())) {
            return Callback.fail(ResponseEnum.FAIL.getCode(), ResponseEnum.FAIL.getMemo(), "问诊单进度重复操作");
        }
        // 判断当前进度的下一步进度是否对应 如果不对应 返回错误
        if (nowStep.getNextStep() != nextStep.getOrder()) {
            return Callback.fail(ResponseEnum.FAIL.getCode(), ResponseEnum.FAIL.getMemo(), "禁止跨进度,当前进度下一步非:" + nextStep.getCode());
        }

        organizationService.addConsultationSheet(consultationSheet, consultationSheetSchedule);
        return Callback.success(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMemo());

    }


    @Require
    @PostMapping("/head/img")
    @ApiOperation("获取用户的头像")
    public JsonData getUserHeadImg(HttpServletRequest request, HttpServletResponse response,
                                   @ApiParam(name = "sheetId", value = "问诊单的id") @RequestBody List<String> uuids) {
        String uuid = request.getAttribute("uuid").toString();
        if (uuids.size() == 0) {
            return Callback.fail(ResponseEnum.FAIL.getCode(), "uuid列表不能为空");
        }
        return Callback.success(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMemo(), organizationService.getUserHeadImg(uuids));

    }
}
